package com.taplinker.client.core;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.taplinker.client.domain.LoginUser;
import com.taplinker.core.Resource;
import com.taplinker.core.ResourceManager;
import com.taplinker.core.util.json.JsonUtil;

public class LoginHistoryManager implements Resource {
	private static final String FILE_NAME = "aconut_login";
	private static final String LOGIN_USERS = "login_users";
	private static final String LAST_LOGIN_INDEX = "last_login_index";
	
	private List<LoginUser> userList = new ArrayList<LoginUser>();
	private static LoginHistoryManager instance;
	
	private LoginHistoryManager() {
	}
	
	public static LoginHistoryManager getInstance() {
		if(instance != null) {
			return instance;
		}
		synchronized (LoginHistoryManager.class) {
			if(instance == null) {
				instance = new LoginHistoryManager();
				ResourceManager.getInstance().addResource(instance);
			}
		}
		return instance;
	}
	
	public LoginUser getLast(Context ctx) {
		if(ctx == null) {
			return null;
		}
		SharedPreferences sp = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		int lastIndex = sp.getInt(LAST_LOGIN_INDEX, -1);
		if(lastIndex == -1) {
			return null;
		}
		String jstr = sp.getString(LOGIN_USERS, "");
		if(TextUtils.isEmpty(jstr)) {
			return null;
		}
		userList.clear();
		userList = JsonUtil.jsonToList(jstr, LoginUser.class);
		if(userList.size() <= 0) {
			return null;
		}
		if(lastIndex > userList.size()) {
			return null;
		}	
		LoginUser lastUser = userList.get(lastIndex);
		if(lastUser == null) {
			return null;
		}
		return lastUser;
	}
	
	public List<LoginUser> getAll(Context ctx) {
		if(ctx == null) {
			return null;
		}
		SharedPreferences sp = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		int lastIndex = sp.getInt(LAST_LOGIN_INDEX, -1);
		if(lastIndex == -1) {
			return null;
		}
		String jstr = sp.getString(LOGIN_USERS, "");
		if(TextUtils.isEmpty(jstr)) {
			return null;
		}
		userList.clear();
		userList = JsonUtil.jsonToList(jstr, LoginUser.class);
		return userList;
	}
	
	public void save(Context ctx, LoginUser user) {
		if(ctx == null || user == null) {
			return;
		}
		SharedPreferences sp = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		String jstr = sp.getString(LOGIN_USERS, "");
		if(!TextUtils.isEmpty(jstr)) {
			userList.clear();
			userList = JsonUtil.jsonToList(jstr, LoginUser.class);
			if(userList != null) {
				for(LoginUser u : userList) {
					if(!TextUtils.isEmpty(u.getName()) && u.getName().equals(user.getName())) {
						userList.remove(u);
						break;
					}
				}
			}
		}
		if(!user.isRemember()) {
			user.setPassword("");
		}
		userList.add(user);
		int lastIndex = userList.indexOf(user);
		Editor editor = sp.edit();
		editor.putString(LOGIN_USERS, JsonUtil.listToJson(userList));
		editor.putInt(LAST_LOGIN_INDEX, lastIndex);
		editor.commit();
		editor.clear();
	}
	
	public void delete(Context ctx, LoginUser user) {
		if(ctx == null || user == null) {
			return;
		}
		SharedPreferences sp = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		String jstr = sp.getString(LOGIN_USERS, "");
		if(TextUtils.isEmpty(jstr)) {
			return;
		}
		int lastIndex = 0;
		userList.clear();
		userList = JsonUtil.jsonToList(jstr, LoginUser.class);
		if(userList == null ) {
			return;
		}
		for(LoginUser u : userList) {
			if(!TextUtils.isEmpty(u.getName()) && u.getName().equals(user.getName())) {
				lastIndex = userList.indexOf(u);
				userList.remove(u);
				break;
			}
		}
		int lastIndexOld = sp.getInt(LAST_LOGIN_INDEX, -1);
		if(lastIndexOld == -1 || lastIndexOld == lastIndex) {
			lastIndex = 0;
		} else {
			lastIndex = lastIndexOld;
		}
		Editor editor = sp.edit();
		editor.putString(LOGIN_USERS, JsonUtil.listToJson(userList));
		editor.putInt(LAST_LOGIN_INDEX, lastIndex);
		editor.commit();
		editor.clear();
	}
	
	public void reset(Context ctx, LoginUser user) {
		if(ctx == null || user == null) {
			return;
		}
		user.setAutoLogin(false);
		user.setRemember(false);
		user.setPassword("");
		save(ctx, user);
	}

	@Override
	public void clear() {
		
	}

	@Override
	public void releaseResource() {
		
	}
}
